package com.lazily.set_.exercise_;


import java.util.HashSet;
import java.util.Objects;

/**
 * @Author: GZQ
 * @date: 2025/5/27 14:51
 * @description: HashSet练习巩固
 * @version: 1.0
 */

@SuppressWarnings({"all"})
public class HashSetExercise01 {
	// 程序入口
	public static void main(String[] args) {
		/*
			定义一个employee类，该类包含: private 成员属性 name sal birthday(MyDate)类型，
			其中 birthday 为 MyDate类型(属性包括: year, month, day), 要求
			1. 创建3个 employee 放入 HashSet 中
			2. 当name 和 birthday 的值相同时，认为是相同员工, 不能放入到Hashset中
		 */

		HashSet hashSet = new HashSet();
		hashSet.add(new Employee("jack", 3000, new MyDate(1999, 12, 12)));
		hashSet.add(new Employee("jack", 3000, new MyDate(1999, 12, 12)));
		hashSet.add(new Employee("jac", 3000, new MyDate(1999, 12, 12)));
		System.out.println("HashSet: " + hashSet);
	}
}

@SuppressWarnings({"all"})
class Employee {
	private String name;
	private double sal;
	private MyDate birthday;

	public Employee(String name, double sal, MyDate birthday) {
		this.name = name;
		this.sal = sal;
		this.birthday = birthday;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSal() {
		return sal;
	}

	public void setSal(double sal) {
		this.sal = sal;
	}

	public MyDate getBirthday() {
		return birthday;
	}

	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}

	// 如果 name 和 birthday 值相同，则返回相同的 hash 值

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Employee employee = (Employee)o;
		return Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, birthday);
	}

	@Override
	public String toString() {
		return "Employee{" +
				"name='" + name + '\'' +
				", sal=" + sal +
				", birthday=" + birthday +
				'}';
	}
}


@SuppressWarnings({"all"})
class MyDate {
	private int year;
	private int month;
	private int day;

	public MyDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	// 如果 year, month, day 值相同，则返回相同的 hash 值

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		MyDate myDate = (MyDate)o;
		return year == myDate.year && month == myDate.month && day == myDate.day;
	}

	@Override
	public int hashCode() {
		return Objects.hash(year, month, day);
	}

	@Override
	public String toString() {
		return "MyDate{" +
				"year=" + year +
				", month=" + month +
				", day=" + day +
				'}';
	}
}
